package simpledb;

import java.sql.*;
import simpledb.remote.SimpleDriver;

public class Fourjoins {
    public static void main(String[] args) {
//              String major = args[0];
//              String major = "art";
                System.out.println("Here are the dnames");
                System.out.println("sid \t dname");

                Connection conn = null;
                try {
                        // Step 1: connect to database server
                        Driver d = new SimpleDriver();
                        conn = d.connect("jdbc:simpledb://localhost", null);

                        // Step 2: execute the query
                        Statement stmt = conn.createStatement();
                        String qry = "select sid, dname "
                                   + "from student, enroll, section, course, dept "
                                   + "where sid = studentid "
                                   + "and sectionid = sectid "
                                   + "and courseid = cid "
                                   + "and deptid = did";
                        ResultSet rs = stmt.executeQuery(qry);

                        // Step 3: loop through the result set
                        while (rs.next()) {
                                int sid = rs.getInt("sid");
                                String dname = rs.getString("dname");
                                System.out.println(sid + "\t" + dname);
                        }
                        rs.close();
                }
                catch(Exception e) {
                        e.printStackTrace();
                }
                finally {
                        // Step 4: close the connection
                        try {
                                if (conn != null)
                                        conn.close();
                        }
                        catch (SQLException e) {
                                e.printStackTrace();
                        }
                }
        }
}